import requests
from bs4 import BeautifulSoup
from src.common.logger import getLogger
from src.common.ScheduleMessage import ScheduleMessage

logger = getLogger()

def snatch_current_new():
    # 1. 发送请求获取网页内容
    url = "https://www.ithome.com/"
    logger.info(f"snatch_current_new url: {url}")
    response = requests.get(url)
    response.raise_for_status()  # 检查请求是否成功
    response.encoding = 'utf-8'

    # 2. 解析 HTML
    soup = BeautifulSoup(response.text, 'lxml')  # 或 'html.parser'

    # 3. 提取数据
    title = soup.title.string if soup.title else "无标题"
    paragraphs = [p.get_text(strip=True) for p in soup.find_all('p')]
    logger.info(f"snatch_current_new title: {title}")

    results = []
    for i, para in enumerate(paragraphs[:10], 1):
        results.append(para)
    return results

def snatch_current_weather(args):
    cityCode = args.get("cityCode")
    response = requests.get(f"http://t.weather.sojson.com/api/weather/city/{cityCode}", timeout = 5)
    return response.json()

def snatch_refresh_statistics(args):
    schedule_message = ScheduleMessage()
    user_statistics = schedule_message.message_user_count()
    history_memory_statistics = schedule_message.message_dialog_response()
    return { **user_statistics, **history_memory_statistics }
